package org.gap.web.jsf;

import java.util.List;

import org.gap.dao.AdherentDAO;
import org.gap.dao.TiersDAO;

import org.gap.domain.Adherent;
import org.gap.domain.Tiers;

import org.gap.service.AdherentService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring Component that handles CRUD requests for Adherent entities
 * 
 */
@Scope("session")
@Component("AdherentComponent")
public class AdherentComponentImpl implements AdherentComponent {

	/**
	 * Data type variable that provides CRUD operations for Adherent entities
	 * 
	 */
	private Adherent adherent;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	private Tiers tiers;

	/**
	 * DAO injected by Spring that manages Adherent entities
	 * 
	 */
	@Autowired
	private AdherentDAO adherentDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Adherent entities
	 * 
	 */
	@Autowired
	private AdherentService adherentService;

	/** 
	 */
	public AdherentComponentImpl() {
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public String saveAdherentTiers(Integer adherent_adhId, Tiers tiers) {
		adherent = adherentService.saveAdherentTiers(adherent_adhId, tiers);

		return "/jsf/adherent/viewAdherent.xhtml";
	}

	/**
	 * Getter for variable Adherent
	 * 
	 */
	@Transactional
	public Tiers getTiers() {
		return tiers;
	}

	/**
	 * Get Tiers entity by Adherent
	 * 
	 */
	@Transactional
	public Tiers getAdherentTiers(Integer adhIdKey) {
		return adherentDAO.findAdherentByPrimaryKey(adhIdKey).getTiers();
	}

	/**
	 * Select the child Tiers entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@Transactional
	public String confirmDeleteAdherentTiers(Integer adherent_adhId, Integer related_tiers_tieTiers) {
		tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers);

		return "/jsf/adherent/tiers/deleteTiers.xhtml";
	}

	/**
	 * Create a new Adherent entity
	 * 
	 */
	@Transactional
	public String newAdherent() {
		adherent = new Adherent();

		return "/jsf/adherent/createAdherent.xhtml";
	}

	/**
	 * Edit an existing Adherent entity
	 * 
	 */
	@Transactional
	public String editAdherent(Integer adhIdKey) {
		adherent = adherentDAO.findAdherentByPrimaryKey(adhIdKey);

		return "/jsf/adherent/editAdherent.xhtml";
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public String deleteAdherentTiers(Integer adherent_adhId, Integer related_tiers_tieTiers) {
		adherent = adherentService.deleteAdherentTiers(adherent_adhId, related_tiers_tieTiers);

		return "/jsf/adherent/viewAdherent.xhtml";
	}

	/**
	 * Create a new Tiers entity
	 * 
	 */
	@Transactional
	public String newAdherentTiers(Integer adherent_adhId) {
		tiers = new Tiers();

		return "/jsf/adherent/tiers/createTiers.xhtml";
	}

	/**
	 * Delete an existing Adherent entity
	 * 
	 */
	@Transactional
	public String deleteAdherent(Integer adhIdKey) {
		Adherent adherent = adherentDAO.findAdherentByPrimaryKey(adhIdKey);
		adherentService.deleteAdherent(adherent);
		return "/jsf/adherent/listAdherents.xhtml";
	}

	/**
	 * Show all Adherent entities
	 * 
	 */
	@Transactional
	public List<Adherent> listAdherents() {
		return new java.util.ArrayList<Adherent>(adherentService.loadAdherents());
	}

	/**
	 * Select the Adherent entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@Transactional
	public String confirmDeleteAdherent(Integer adhIdKey) {
		adherent = adherentDAO.findAdherentByPrimaryKey(adhIdKey);

		return "/jsf/adherent/deleteAdherent.xhtml";
	}

	/**
	 * Edit an existing Tiers entity
	 * 
	 */
	@Transactional
	public String editAdherentTiers(Integer adherent_adhId, Integer related_tiers_tieTiers) {
		tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers, -1, -1);
		return "/jsf/adherent/tiers/editTiers.xhtml";
	}

	/**
	 * Select an existing Adherent entity
	 * 
	 */
	@Transactional
	public String selectAdherent(Integer adhIdKey) {
		adherent = adherentDAO.findAdherentByPrimaryKey(adhIdKey);

		return "/jsf/adherent/viewAdherent.xhtml";
	}

	/**
	 * View an existing Tiers entity
	 * 
	 */
	@Transactional
	public String selectAdherentTiers(Integer adherent_adhId, Integer related_tiers_tieTiers) {
		tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers, -1, -1);
		return "/jsf/adherent/tiers/viewTiers.xhtml";
	}

	/**
	 * Getter for variable Adherent
	 * 
	 */
	@Transactional
	public Adherent getAdherent() {
		return adherent;
	}

	/**
	 * Save an existing Adherent entity
	 * 
	 */
	@Transactional
	public String saveAdherent(Adherent adherent) {
		adherentService.saveAdherent(adherent);
		return "/jsf/adherent/listAdherents.xhtml";
	}

}
